jQuery(document).ready(function(){	
	jQuery('#applyTube').click(function(){					
		var action = jQuery('#action').val();
		if(action=='0'){
			alert('Please choose action !');
		}else {
			var ids = '';
			var count = 0;
			jQuery('#table-result tbody tr td input[type=checkbox]').each(function (item, data) {
				if (jQuery(this).is(':checked')) {				
					ids += jQuery(this).val() + ',';
					count++;
				}
			});		
			if(count>0){
				if(action=='1'){
					if(confirm('All checked tubes will be removed from slideshow !')){
						showAjaxEffect();
						jQuery.get('/admin/tube/process', {ids: ids, action: 'remove-slideshow-all'}, function(data) {
							hideAjaxEffect();
							scrollToTop();
							if(data==''){
								jQuery('#table-result tbody tr td input[type=checkbox]').each(function (item, data) {
									if (jQuery(this).is(':checked')) {				
										jQuery(this).parents("tr").remove();						
									}
								});
								jQuery('#messageDiv').fadeIn();
								jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
								jQuery('#messageDiv > p').html('Action success ! Tubes have been deleted');
								setTimeout(function() {
									jQuery('#messageDiv').fadeOut();
								}, 2000);	
							}else{
								jQuery('#messageDiv').fadeIn();
								jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
								jQuery('#messageDiv > p').html(data);
								setTimeout(function() {
									jQuery('#messageDiv').fadeOut();
								}, 2000);	
							}							
						});
					}else{
						return false;
					}	
				}							
			}else{
				alert('Please check some tubes to do this action !');
			}				
		}
	});							
	jQuery('.btnDelete').click(function(){	
		var temp = jQuery(this);
		if(confirm('Are you sure want to delete this tube ?')){				
			showAjaxEffect();
			jQuery.get('/admin/tube/process', { 
				action: 'delete', 
				id: jQuery(this).attr('href'), 
					} ,
				function( data ) {
					hideAjaxEffect();
					scrollToTop();
					if(data==''){							
						temp.parents('tr').remove();	
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
						jQuery('#messageDiv > p').html('Action success ! Tube has been deleted !');
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}else{
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
						jQuery('#messageDiv > p').html(data);
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}	
	     		}
			);				
		}
		return false;
	}); 
	jQuery('.btnRemove').click(function(){	
		var temp = jQuery(this);
		if(confirm('Are you sure want to remove this tube from slideshow ?')){				
			showAjaxEffect();
			jQuery.get('/admin/tube/process', { 
				action: 'remove-slide', 
				id: jQuery(this).attr('href'), 
					} ,
				function( data ) {
					hideAjaxEffect();
					scrollToTop();
					if(data==''){							
						temp.parents('tr').remove();	
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
						jQuery('#messageDiv > p').html('Action success ! Tube has been removed !');
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}else{
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
						jQuery('#messageDiv > p').html(data);
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}	
	     		}
			);				
		}
		return false;
	}); 
	jQuery('#sortable').sortable({			
		opacity: 0.6,	
		start: function(event, ui){				
		},  update: function(event, ui) {
			showAjaxEffect();
			var currentId = jQuery(ui.item).attr('id');
			var prevId = jQuery(ui.item).prev().attr('id');
			var nextId = jQuery(ui.item).next().attr('id');
			jQuery.get('/admin/tube/process', {
				currentId:currentId,
				prevId:prevId,
				nextId:nextId,
				action: 'order-slide'
			}, function(data) {
				
			});			
        },
		stop : function(event, ui){				
			hideAjaxEffect();
			jQuery(ui.item).effect('highlight', {color: '#66CC66'}, 1500);  					
        }

	});	
	jQuery('#sortable').disableSelection();
});